package server.ppt;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class PptLauncher {

	private ActiveXComponent pptApp;
	private ActiveXComponent presentation;
	public static final int msoTrue = -1;
	public static final int msoFalse = 0;

	public PptLauncher(String filePath, boolean isVisible) throws Exception {

		if (this.pptApp != null || this.presentation != null)
			this.closePpt();
		
		if (null == filePath || "".equals(filePath)) {
			throw new Exception("文件路径为空!");
		}
		File file = new File(filePath);
		if (!file.exists()) {
			throw new Exception("文件不存在!");
		}
		
		pptApp = new ActiveXComponent("PowerPoint.Application");
		if (isVisible)
			pptApp.setProperty("Visible", new Variant(true));
		ActiveXComponent presentations = pptApp
				.getPropertyAsComponent("Presentations");
		presentation = presentations.invokeGetComponent("Open", new Variant(
				filePath), new Variant(msoTrue), new Variant(msoTrue),
				new Variant(msoFalse));
	}

	public void show() {
		ActiveXComponent setting = presentation
				.getPropertyAsComponent("SlideShowSettings");
		setting.invoke("Run");
		ComThread.Release();
	}

	public void closePpt() throws Exception {
		if (presentation != null) {
			Dispatch.call(presentation, "Close", new Variant(true));
		}
		pptApp.invoke("Quit", new Variant[] {});
		ComThread.Release();
		this.presentation = null;
		this.pptApp = null;
	}

	public void startCapture(String path, String scaledPath) {
		new PptCapturer(this.pptApp, this.presentation, path, scaledPath)
				.start();
	}

	public static void main(String[] strs) throws Exception {
		PptLauncher launcher = new PptLauncher(
				"C://Users/Administrator/Desktop/工作安排.pptx", false);
		launcher.startCapture("E://test", "E://sliande/");
		// launcher.closePpt();
		launcher.show();
	}
}